﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using LibCommon;
using RTCCore;

namespace LibRTC.Core.MessageMap.RegistrationStateChangeEvent
{
    public class AuthHandler : SimpleMessageHandler
    {
        private static ILogger LOGGER = LoggerFactory.GetLogger("AuthHandler", null);

        private RTCCoreClient _coreClient;
        private RTCSystem _rtcSystem = RTCSystem.GetInstance();

        public AuthHandler()
        {
            _rtcEvent = RTC_EVENT.RTCE_REGISTRATION_STATE_CHANGE;
            _name = "AuthHandler";
        }

        public void SetRTCCoreClient(RTCCoreClient client)
        {
            Debug.Assert(null != client);
            _coreClient = client;
        }

        public override void Process(RTC_EVENT rtcEventType, object rtcEvent)
        {
            Debug.Assert(RTC_EVENT.RTCE_REGISTRATION_STATE_CHANGE == rtcEventType);
            Debug.Assert(null != rtcEvent);
            Debug.Assert(rtcEvent is IRTCRegistrationStateChangeEvent);

            IRTCRegistrationStateChangeEvent registerEvent = (IRTCRegistrationStateChangeEvent) rtcEvent;

            LOGGER.LogDebug("" + registerEvent.State);

            switch (registerEvent.State) {

                case RTC_REGISTRATION_STATE.RTCRS_REJECTED:
                case RTC_REGISTRATION_STATE.RTCRS_ERROR:
                    {
                        LOGGER.LogDebug(""+registerEvent.State);

                        int statusCode = registerEvent.StatusCode;

                        LOGGER.LogDebug("RTCRS_REJECTED/ERROR " + statusCode);

                        // Logon failed. Most likely the server could no be found,
                        // or the user needs to authenticate.
                        if ((statusCode == RTCConst.RTC_E_STATUS_CLIENT_UNAUTHORIZED) ||
                            (statusCode == RTCConst.RTC_E_STATUS_CLIENT_PROXY_AUTHENTICATION_REQUIRED) ||
                            (statusCode == RTCConst.RTC_E_STATUS_CLIENT_FORBIDDEN) ||
                            (statusCode == RTCConst.RTC_E_STATUS_CLIENT_NOT_FOUND))
                        {

                            _coreClient.Profile.SetCredentials(_rtcSystem.LocalUserURI, _rtcSystem.LocalUserName, _rtcSystem.LocalUserPassword);
                            _coreClient.DoRegister();

                        }
                        else
                        {
                            _coreClient.ILogOnUI.LogOnError();

                            // Logon failed
                            _coreClient.DoLogOff();
                            _coreClient.DoDisablePresence();
                            _coreClient.IPresenceUI.OnLoggedOff();
                            // If we were logging on the show error
                            if (RTC_REGISTRATION_STATE.RTCRS_REGISTERING == _coreClient.RegistrationState)
                            {
                                _coreClient.IPresenceUI.ShowMessage("Logon failed!", "Warning");
                            }
                            return;
                        }
                    }
                    break;

                default:
                    break;
            }

            _coreClient.RegistrationState = registerEvent.State;
        }
    }
}
